#include "bomb.h"
#include "../game/config.h"
#include "../ui/text.h"
#include <stdio.h>
#include<pthread.h>
#include "../utils/timer.h" 
#define VAR_NAME(BUF,VAR) sprintf(BUF,"%s",#VAR);
struct Bomb _bomb= {._init=_initBombS,
             ._show=_showBombS
            }; 
struct Bomb directB1; 
BombS _initBombS(int posx,int posy,int type,char name[20]) {
	int lifeType;
	int attackType;
	int attackRangeType;
	char infoType[30];
	char drawType[4];
	switch(type) {
		case DirectBombT:

			lifeType = 3000;
			attackType = 5;
			attackRangeType = 0;
			strcpy(infoType,"c90 attack skill0");
			strcpy(drawType,B1);

			break;
	}

	BombS bomb= {
		.pos = {.x=posx,.y=posy},
		.maxlife=lifeType,
		.attack=attackType,
		.coldtime=5,
		.type=type,
		.moveSpeed=1,
		.curlife=lifeType,
		.level=1,
		.exp = 1,
		.boomAttack=0.1,
		.attackRange=attackRangeType,
		strcpy(bomb.info,infoType),
		strcpy(bomb.draw,drawType),
		strcpy(bomb.name,name)
	};
	return bomb;
}


int isInAtRangeBomb(BombS org,struct V2s otherV2s) {
	int isInAtRange = 0;
	int atRange = org.attackRange;
	double dis = (_vector2._getDis(org.pos,otherV2s));
	if(dis <= atRange) isInAtRange = 1;
	else isInAtRange = 0;

	return isInAtRange;
}

int isInMap(int x, int y) {
	if(x <= 0 ||x > W || y <= 0||y > H)
		return 0;
	return 1;
}
int attackBomb(BombS goS,HeroS *other) {
	/*gotoxy(0,H+8);
	printf("%d:%d",other->pos.x,other->pos.y);
	printf("\n%d:%d",goS.pos.x,goS.pos.y);*/

	if(isInAtRangeBomb(goS,other->pos)) {
		int realAttack = goS.attack - other->defense;
		realAttack = realAttack>0?realAttack:1;
		if(other->curlife >= realAttack)
			other->curlife = other->curlife - realAttack;
		//clear bomb
		return 1;
	}
	return 0;
}


void* startBomb(void* arg) {

	int ix=0,iy=0;
	BombS bs = directB1.bombS;
	int timer = bs.curlife;
	struct V2s v2s = {.x=bs.pos.x,.y=bs.pos.y};
	struct V2s v2sAdd;
	switch(player.heros.dir) {
		case eUp:
			v2sAdd.x=0;
			v2sAdd.y=-1;
			break;
		case eRight:
			v2sAdd.x=1;
			v2sAdd.y=0;
			break;
		case eDown:
			v2sAdd.x=0;
			v2sAdd.y=1;
			break;
		case eLeft:
			v2sAdd.x=-1;
			v2sAdd.y=0;
			break;

	}
	while(timer >0 && isInMap(bs.pos.x,bs.pos.y)) {

		gotoxy(v2s.x+ix,v2s.y+iy);
		bs.pos.x = v2s.x+ix;
		bs.pos.y = v2s.y+iy;
		printf(B1);
		fflush(stdout);
		sleep_ms(60);
		timer-=60;
		int ret = attackBomb(bs,&enemy.heros);
		if(ret) return NULL;
		//ix++;
		ix+=v2sAdd.x;
		iy+=v2sAdd.y;
	}

}
void _showBombS(BombS bombS) {

	// showBomb(bombS->pos,bombS->draw);
	pthread_t ntid;


	pthread_create(&ntid,NULL,startBomb,NULL);

	pthread_detach(ntid);
}

void showBomb(struct V2s v2s,const char *str) {
	gotoxy(v2s.x,v2s.y);
	puts(str);
}
void showBombAttackRange(BombS bombS,char rangeStr[4]) {

	for(int i=0; i<4; i++) {
		for(int j=0; j<=bombS.attackRange; j++) {
			struct V2s v2sRange = bombS.pos;
			getPosByDir(&v2sRange,i,j);
			gotoxy(v2sRange.x,v2sRange.y);
			puts(rangeStr);
		}
	}
}



void initBomb(struct V2s pos, int type) {
	char buf[20];
	VAR_NAME(buf,directB1);
	BombS bombS = _bomb._init(pos.x,pos.y, type,buf);
	_bomb.bombS = bombS;
	directB1 = _bomb;
}
void initBombs(struct V2s pos) {
	initBomb(pos,DirectBombT);

}
